package com.polaris.live.viewmodel.live

import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.constant.UserConst
import com.polaris.live.net.homeService
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.go_resp.MatchStartBean
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.listeners.OnStreamLiveDataListener
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * LiveBroadViewModel
 *
 * @author Created by 半仙 on 2023/10/13/013 16:59
 */
class LiveBroadViewModel : BaseViewModel(), OnStreamLiveDataListener {

    /**
     * 开始匹配
     */
    fun matchStart(context: Context, type: Int) {
        request({ homeService.matchStart(MatchStartBean(type, UserConst.From.LIVE)) }, {
            GlobalPopupManager.showCompletePopup(context, isOk = {
                Router.builder(RoutePath.LIVE_MATCH_ACTIVIYT)
                    .withInt("page", UserConst.From.LIVE)
                    .withInt("lastPage", UserConst.LastFrom.LIVE)
                    .withLong("id", it.id)
                    .navigation()
            })
        })
    }

    //直播间滑动更多逻辑
    val liveLoadMoreLiveData = MutableLiveData<ResultState<DisplayUserBean>>()
    fun getLoadMore(userId: Long?, delay: Long? = null) {
        viewModelScope.launch {
            if (delay != null) {
                delay(delay)
            }
            request({ homeService.getLiveSingle(1, userId) }, liveLoadMoreLiveData)
        }
    }


    override val userLiveLiveData: MutableLiveData<DisplayUserBean> = MutableLiveData()
}